﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Dynamic;
using Models;
using Newtonsoft.Json;

public partial class Admin_News_choose : AdminBase
{
    Entities db = new Entities();
    HashSet<int> selects_ids = new HashSet<int>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ids"]))
            {
                var ids = Request.QueryString["ids"].Split(',');
                if (ids.Any())
                {
                    int i = 0;
                    foreach (var id in ids)
                    {
                        if (int.TryParse(id, out i))
                        {
                            selects_ids.Add(i);
                        }
                    }
                }
            }
        }
        jsTreeJson.Text = getTree();
    }

    protected string getTree()
    {
       
        dynamic JsTree = new ExpandoObject();
        var list = new List<object>();

        var m = from n in db.L_Classs.Where(x => x.CpareID == 0 ) select n;
        foreach (var n in m)
        {

            if (chkpow(n.CID))
            {
            
            list.Add(new
            {
                id = "node_" + n.CID,
                parent = "#",
                text = n.CNmae,
                state = new
                {
                    opened = true,
                    selected = selects_ids.Contains(n.CID)
                },
                a_attr = new
                {
                    link = "#"
                }
            });
            getChildrenNodes(list, n.CID);
            }
        }
        JsTree.data = list;
        return JsonConvert.SerializeObject(JsTree);
    }

    protected void getChildrenNodes(List<object> nodes, long cID)
    {
        var mm = from n in db.L_Classs.Where(x => x.CpareID == cID) select n;
        string parent = "node_" + cID;
        foreach (var n in mm)
        {
            if (chkpow(n.CID))
            {
                nodes.Add(new
                {
                    id = "node_" + n.CID,
                    parent = parent,
                    text = n.CNmae,
                    state = new
                    {
                        opened = false,
                        selected = selects_ids.Contains(n.CID)
                    },
                    a_attr = new
                    {
                        link = "#"
                    }
                });
                getChildrenNodes(nodes, n.CID);
            }
        }
    }



    public bool chkpow(long claid)
    {
        if (sysset.NewsRule)
        { return true; }
        else
        {
            string admin_power = "";
            int _Id = int.Parse(Session["AdminID"].ToString());
            using (Entities db = new Entities())
            {
                admin_power = db.Manages.SingleOrDefault(p => p.MID == _Id).MRole;
            }

            if (admin_power.IndexOf("," + claid + ",") > -1)
                return true;
            else
                return false;
        }
    }


}